package com.lsmgc.cms.servlet.poster;

import com.lsmgc.cms.entity.poster.Poster;
import com.lsmgc.cms.entity.poster.PosterQuery;
import com.lsmgc.cms.service.poster.PosterService;
import com.lsmgc.cms.util.RequestUtils;
import com.lsmgc.cms.util.ServletUtils;
import com.lsmgc.cms.util.page.Pagination;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

public class AdminPosterServlet extends HttpServlet {
    private PosterService posterService = new PosterService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/admin/poster/list":
                try {
                    posterList(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/poster/add":
                posterAdd(request, response);
                break;
            case "/admin/poster/upd":
                try {
                    posterUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/poster/list":
                ajaxPosterList(request, response);
                break;
            case "/admin/ajax/poster/add":
                try {
                    ajaxPosterAdd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/poster/upd":
                try {
                    ajaxPosterUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/poster/del":
                try {
                    ajaxPosterDel(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void posterList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        PosterQuery posterQuery = new PosterQuery();
        posterQuery.setName(request.getParameter("name"));
        posterQuery.setPageNo(Pagination.cpn(RequestUtils.getInt(request, "pageNo")));
        posterQuery.setPageSize(5);
        Pagination pagination = posterService.getPosterListPage(posterQuery);
        request.setAttribute("pagination", pagination);
        request.setAttribute("posterQuery", posterQuery);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/poster/poster-list")).forward(request, response);
    }

    private void posterAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/poster/poster-add")).forward(request, response);
    }

    private void posterUpd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Long id = RequestUtils.getLong(request, "id");
        Poster poster = posterService.getPosterById(id);
        request.setAttribute("poster", poster);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/poster/poster-upd")).forward(request, response);
    }

    private void ajaxPosterList(HttpServletRequest request, HttpServletResponse response) {

    }

    private void ajaxPosterAdd(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        Poster poster = new Poster();
        poster.setName(request.getParameter("name"));
        poster.setImg(request.getParameter("img"));
        poster.setLink(request.getParameter("link"));
        poster.setAddTime(new Date());
        boolean b = posterService.addPoster(poster);
        if (b) {
            ServletUtils.renderJson(response, ServletUtils.getYesJson());
        } else {
            ServletUtils.renderJson(response, ServletUtils.getNoJson());
        }
    }

    private void ajaxPosterUpd(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        Poster poster = new Poster();
        poster.setId(RequestUtils.getLong(request, "id"));
        poster.setName(request.getParameter("name"));
        poster.setImg(request.getParameter("img"));
        poster.setLink(request.getParameter("link"));
        boolean b = posterService.updatePosterById(poster);
        if (b) {
            ServletUtils.renderJson(response, ServletUtils.getYesJson());
        } else {
            ServletUtils.renderJson(response, ServletUtils.getNoJson());
        }
    }

    private void ajaxPosterDel(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        Long id = RequestUtils.getLong(request, "id");
        boolean b = posterService.deletePosterById(id);
        if (b) {
            ServletUtils.renderJson(response, ServletUtils.getYesJson());
        } else {
            ServletUtils.renderJson(response, ServletUtils.getNoJson());
        }
    }
}
